package com.yangye.otherdemo.volatiledemo;

/**
 * Created by yangye on 2019/7/1
 */
public class StoppableThread extends Thread {

	//	private static volatile boolean stoppable;
	private static boolean stoppable;

	@Override
	public void run() {
		System.out.println(Thread.currentThread() + "等待停止");
		while (true) {
			if (stoppable) {
				System.out.println(Thread.currentThread() + " -- 停止了");
				break;
			}
		}
	}

	public void tellMeToStop() throws InterruptedException {
		int sec = 3;
		System.out.println(sec + "秒后可以停止");
		Thread.sleep(sec * 1000);
		this.stoppable = true;
	}

	public static void main(String[] args) throws InterruptedException {
		StoppableThread t1 = new StoppableThread();
		StoppableThread t2 = new StoppableThread();
		t2.start();
		t1.tellMeToStop();
	}
}
